/*

COMP 353F
Dr. B Desai
Final Project
Group #11

6330746 Nicholas CONSTANTINIDIS
9532862 Jacqueline FU
5484537 Claudio Javier LOPEZ FLORES
9218416 Que Tung NGUYEN

*/

// Validate that two given fields have the same value
function ValidateFieldsMatch(formName, fieldName1, fieldName2, message) {
	var fieldValue1;
	var fieldValue2;
	try
	{
		fieldValue1 = document.forms[formName][fieldName1].value;
		fieldValue2 = document.forms[formName][fieldName2].value;
	}
	catch(err)
	{
		fieldValue1 = document.getElementsByName(fieldName1)[0].value;
		fieldValue2 = document.getElementsByName(fieldName2)[0].value;
	}
	
	if ((fieldValue1 == null || fieldValue1 == "") && (fieldValue2 == null || fieldValue2 == ""))
	{
		return true;
	}
	else if (fieldValue1 == fieldValue2)
	{
		return true;
	}
	else
	{
		alert(message);
		return false;
	}
}

// Validate that a given mandatory field has been filled out
function ValidateFieldNotEmpty(formName, fieldName, fieldDescription) {
	var fieldValue;
	try
	{
		fieldValue = document.forms[formName][fieldName].value;
	}
	catch(err)
	{
		fieldValue = document.getElementsByName(fieldName)[0].value;
	}
	
	if (fieldValue == null || fieldValue == "")
	{
		alert("You must enter the " + fieldDescription);
		return false;
	}
	else
	{
		return true;
	}
}

// Validate that a given numerical field is numeric
function ValidateFieldNumeric(formName, fieldName, fieldDescription) {
	var fieldValue;
	try
	{
		fieldValue = document.forms[formName][fieldName].value;
	}
	catch(err)
	{
		fieldValue = document.getElementsByName(fieldName)[0].value;
	}
	
	if (fieldValue == null || fieldValue == "")
	{
		return true;
	}
	else if (isNaN(parseFloat(fieldValue)))
	{
		alert("You must enter a valid number for the " + fieldDescription);
		return false;
	}
	else
	{
		return true;
	}
}

// Validate that a given date field is a valid date
function ValidateFieldDate(formName, fieldName, fieldDescription) {
	var fieldValue;
	try
	{
		fieldValue = document.forms[formName][fieldName].value;
	}
	catch(err)
	{
		fieldValue = document.getElementsByName(fieldName)[0].value;
	}
	
	if (fieldValue == null || fieldValue == "")
	{
		return true;
	}
	else
	{
		var dateParse = new Date(fieldValue);
		
		if (!isNaN(dateParse))
		{
			return true;
		}
		else
		{
			alert("You must enter the " + fieldDescription + " in the following way: year-month-day");
			return false;
		}
	}
}

// Validate that a given numerical field is numeric
function ValidateCheckboxChecked(formName, fieldName, message) {
	var fieldChecked;
	try
	{
		fieldChecked = document.forms[formName][fieldName].checked;
	}
	catch(err)
	{
		fieldChecked = document.getElementsByName(fieldName)[0].checked;
	}
	
	if (fieldChecked == null || fieldChecked == "")
	{
		alert(message);
		return false;
	}
	else if (fieldChecked)
	{
		return true;
	}
	else
	{
		alert(message);
		return false;
	}
}
